package models;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import play.db.ebean.Model;

import com.avaje.ebean.annotation.Formula;
import com.avaje.ebean.validation.NotEmpty;

@Entity
@Table(name = "category", uniqueConstraints = @UniqueConstraint(columnNames = "name", name = "unique_name"))
public class Category extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2773453093315711972L;

	public static Finder<Long, Category> find = new Finder<>(Long.class,
			Category.class);

	@Id
	private Long id;

	@NotEmpty
	private String name;

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "parent_id")
	private Category parent;

	@Formula(select = "parent_id")
	private Long parentId;

	public static void create(Category c) {
		c.save();
	}

	public static void delete(Long id) {
		find.ref(id).delete();
	}

	public static List<Category> rootCategories() {
		return find.where().isNull("parent").findList();
	}

	public List<Category> getChildren() {
		return find.where().eq("parentId", this.getId()).findList();
	}

	public int getNumOfAds(Long id) {
		return Advertisement.find.where().eq("category_id", id).findList()
				.size();
	}

	public int getNumOfProds(Long id) {
		return Product.find.where().eq("category_id", id).eq("type", "product")
				.findList().size();
	}

	public Boolean hasChildren() {
		if (getChildren().size() > 0) {
			return true;
		}
		return false;
	}

	public Boolean hasParent() {
		if (getParent() == null) {
			return false;
		}
		return true;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((getName() == null) ? 0 : getName().hashCode());
		result = prime * result + getChildren().hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Category other = (Category) obj;
		if (getName() == null) {
			if (other.getName() != null) {
				return false;
			}
		} else if (!getName().equals(other.getName())) {
			return false;
		}

		// TODO getChildren
		return true;
	}

}
